import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef XinDongGroupSectionContainerBuilder = Widget Function(
    BuildContext context, int section, int index, dynamic data);

class XinDongGroupSectionContainer extends StatelessWidget {
  final List? dataList; // 数据源
  final EdgeInsetsGeometry? padding; // 内边距
  final IndexedWidgetBuilder? separatorBuilder; // 分割的构建
  final IndexedWidgetBuilder? sectionHeaderBuilder; // 每个分组头部的构建
  final bool scrollable; // 是否允许滚动（默认允许true）
  final double sectionRadius; // 分组圆角（默认0.0f）
  final XinDongGroupSectionContainerBuilder itemBuilder; // 每个item的构建
  const XinDongGroupSectionContainer({
    Key? key,
    required this.dataList,
    required this.itemBuilder,
    this.separatorBuilder,
    this.sectionHeaderBuilder,
    this.scrollable = true,
    this.sectionRadius = 0.0,
    this.padding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MediaQuery.removePadding(
      context: context, // 上下文必传
      removeTop: true, // 要移除的区域
      removeBottom: true, // 要移除的区域
      child: ListView.separated(
        shrinkWrap: !scrollable,
        physics: scrollable ? const AlwaysScrollableScrollPhysics() : const NeverScrollableScrollPhysics(),
        padding: padding,
        itemCount: dataList?.length ?? 0,
        itemBuilder: (BuildContext context, int index) {
          // 带分组标题栏的
          if (sectionHeaderBuilder != null) {
            List sectionList = dataList![index];
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                sectionHeaderBuilder!(context, index),
                XinDongGroupSectionInnerItem(
                  section: index,
                  sectionList: sectionList,
                  itemBuilder: itemBuilder,
                  sectionRadius: sectionRadius,
                ),
              ],
            );
          }
          // 默认
          List sectionList = dataList![index];
          return XinDongGroupSectionInnerItem(
            section: index,
            sectionList: sectionList,
            itemBuilder: itemBuilder,
            sectionRadius: sectionRadius,
          );
        },
        // section分割
        separatorBuilder: separatorBuilder ??
            (BuildContext context, int index) {
              List sectionList = dataList![index];
              return SizedBox(height: sectionList.isEmpty ? 0 : 10);
            },
      ),
    );
  }
}

/// 分组内列表视图
class XinDongGroupSectionInnerItem extends StatelessWidget {
  final int section; // 分组
  final List sectionList; // 数据源
  final double sectionRadius; // 分组圆角（默认0.0f）
  final XinDongGroupSectionContainerBuilder itemBuilder;

  const XinDongGroupSectionInnerItem(
      {Key? key,
      required this.section,
      required this.sectionList,
      required this.sectionRadius,
      required this.itemBuilder})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(sectionRadius)),
      child: ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: sectionList.length,
        itemBuilder: (BuildContext context, int index) {
          return itemBuilder(context, section, index, sectionList[index]);
        },
        // item分割
        separatorBuilder: (BuildContext context, int index) => const SizedBox(height: 0.5),
      ),
    );
  }
}
